package com.zui.webapp.action;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.zui.entity.BaseEntity;
import com.zui.entity.product.Catalog;
import com.zui.manager.SessionManager;
import com.zui.util.JqGridPage;

public class CatalogAction extends BaseJQGridAction {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(CatalogAction.class);
	
	private Catalog catalog;
	private List<Catalog> list;
	public CatalogAction() {}
	public CatalogAction(SessionManager<Catalog, String> sessionManager) {
		this.sessionManager = sessionManager;
	}
	
	@Autowired
	private SessionManager<Catalog, String> sessionManager;

	/***********************************************************
	 * Business Method start
	 **********************************************************/
	public String index() {return "index";}
	public String editNew() {
		catalog = new Catalog();
		return "editNew";
	}
	public String edit() {
		if (catalog.getId()==null)
			return "editNew";
		
		catalog = (Catalog)sessionManager.loadById(Catalog.class, catalog.getId());
		return "edit";
	}
	public String view() {
		if (catalog.getId()==null)
			return "view";
		catalog = (Catalog)sessionManager.loadById(Catalog.class, catalog.getId());
		return "view";
	}
	public String delete() {
		if (jq==null || jq.getId()==null ||jq.getId().trim().equals(""))
			return "index";
		sessionManager.deleteEngtityFlagByIds(Catalog.class, jq.getId().split("\\,"));
		/**
		 * @TODO: success message!
		 */
		return "index";
	}
	public String checkUniqueness() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Catalog.class);
		if (checkedField!=null && !"".equals(checkedField.trim())) {
			
			String methodName = "get" + checkedField.substring(0, 1).toUpperCase() + checkedField.substring(1);
			Method method = Catalog.class.getMethod(methodName);
			criteria.add(Restrictions.eq(checkedField, method.invoke(catalog)));
			valid = !sessionManager.checkExistence(criteria);
		}
		return "valid";
	}
	public String create() {
		catalog.setCreatedByUserLogin(super.getCurrentUserName());
		catalog.setCreatedDate(Calendar.getInstance().getTime());
		catalog.setLastModifiedByUserLogin(super.getCurrentUserName());
		catalog.setLastModifiedDate(Calendar.getInstance().getTime());
		sessionManager.saveEntity(catalog);
		return "view";
	}
	public String update() {

		Catalog tempCatalog = catalog;
		catalog = sessionManager.loadById(Catalog.class, catalog.getId());
		catalog.setName(tempCatalog.getName());
		catalog.setNameEn(tempCatalog.getNameEn());
		catalog.setDescription(tempCatalog.getDescription());
		catalog.setLastModifiedByUserLogin(super.getCurrentUserName());
		catalog.setLastModifiedDate(Calendar.getInstance().getTime());
		sessionManager.saveOrUpdate(catalog);
		return "view";
	}
	public String createAndNew() {
		this.create();
		catalog = null;
		return "editNew";
	}
	public String duplicate() {
		if (catalog.getId()==null)
			return "editNew";
		
		catalog = (Catalog)sessionManager.loadById(Catalog.class, catalog.getId());
		catalog.setId(null);
		catalog.setIsDeleted(BaseEntity.FLAG_N);
		return "editNew";
	}
	/**
	 * search the whole catalog list
	 * @return
	 */
	public String catalogJsonList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Catalog.class);
		if (jq !=null && jqFilters!=null) {
			jq.setGroupCriteria(criteria,jqFilters);
		}
		list = sessionManager.findByCriteriaForJQGrid(criteria,jq == null ? jq = new JqGridPage()
		: jq);
		
		//set null for session scope action
		jqFilters = null;
		return "catalogJsonList";
	}
	public void jqgridOper() {
		switch(jq.getOper()) {
			case del: delete();break;
			case add: create();break;
			case edit: create();break;
			default:break;
		}
	}
	

	/***********************************************************
	 * getters and setters
	 **********************************************************/
	/**
	 * @return the userList
	 */
	public List<Catalog> getlist() {
		return list;
	}
	/**
	 * @param userList the userList to set
	 */
	public void setList(List<Catalog> list) {
		this.list = list;
	}
	/**
	 * @return the catalog
	 */
	public Catalog getCatalog() {
		return catalog;
	}
	/**
	 * @return the list
	 */
	public List<Catalog> getList() {
		return list;
	}
	/**
	 * @param catalog the catalog to set
	 */
	public void setCatalog(Catalog catalog) {
		this.catalog = catalog;
	}
	
}
